package com.example.lin.togglebutton.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.example.lin.togglebutton.R;
import com.example.lin.togglebutton.utils.DensityUtil;

public class GuaGuaView extends View {
    /**
     * 开关的默认宽度
     */
    private static final int DEFAULT_WIDTH = 800;
    /**
     * 开关的默认高度为宽度的1半
     */
    private static final int DEFAULT_HEIGHT = (int) (DEFAULT_WIDTH / 2.5f);
    private final String text;
    private final int textColor;
    private final int guaColor;
    private final float textSize;
    private Paint textPaint;
    private Bitmap guaBitmap;
    private Canvas mCanvas;
    private Paint eraserPaint;

    public GuaGuaView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GuaGuaView);
        text = a.getString(R.styleable.GuaGuaView_text);
        textColor = a.getColor(R.styleable.GuaGuaView_text_color, Color.BLACK);
        guaColor = a.getColor(R.styleable.GuaGuaView_gua_color, Color.GRAY);
        textSize = a.getDimension(R.styleable.GuaGuaView_text_size, DensityUtil.sp2px(context, 30));
        a.recycle();
        initPaint();

    }


    private void initPaint() {
        textPaint = new Paint();
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setTextAlign(Paint.Align.CENTER);
        eraserPaint = new Paint();
        eraserPaint.setStyle(Paint.Style.STROKE);
        eraserPaint.setStrokeWidth(50);
        eraserPaint.setStrokeCap(Paint.Cap.ROUND);
        eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int specModeWidth = MeasureSpec.getMode(widthMeasureSpec);//得到宽度的布局模式
        int specSizeWidth = MeasureSpec.getSize(widthMeasureSpec);//得到布局参数的宽度
        int resultWidth = specSizeWidth;
        switch (specModeWidth) {
            case MeasureSpec.EXACTLY:// 宽高的值是固定的时候,如:match_parent 或 "22dp"
                resultWidth = specSizeWidth;
                break;
            case MeasureSpec.AT_MOST:// 你的宽高的值 为 wrap_content 的时候
                resultWidth = DEFAULT_WIDTH;
                break;
        }
        int specModeHeight = MeasureSpec.getMode(heightMeasureSpec);//得到高度的布局模式
        int specSizeHeight = MeasureSpec.getSize(heightMeasureSpec);//得到布局参数的高度
        int resultHeight = specSizeHeight;
        switch (specModeHeight) {
            case MeasureSpec.EXACTLY:// 宽高的值是固定的时候,如:match_parent 或 "22dp"
                resultHeight = specSizeHeight;
                break;
            case MeasureSpec.AT_MOST:// 你的宽高的值 为 wrap_content 的时候
                resultHeight = DEFAULT_HEIGHT;
                break;
        }
        //调用该方法,最终确定该View的宽高
        setMeasuredDimension(resultWidth, resultHeight);
        initGUaBitmap();
    }


    private void initGUaBitmap() {
        guaBitmap = Bitmap.createBitmap(getMeasuredWidth(), getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(guaBitmap);
        //颜色
        mCanvas.drawColor(guaColor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int cx = getMeasuredWidth() / 2;
        int cy = getMeasuredHeight() / 2;
        Paint.FontMetrics fm = textPaint.getFontMetrics();
        float baselineY = (fm.descent - fm.ascent) / 2 - fm.descent + cy;
        canvas.drawText(text, cx, baselineY, textPaint);
        canvas.drawBitmap(guaBitmap, 0, 0, null);
    }



    private float lastX;
    private float lastY;
    private float currentX;
    private float currentY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        lastX = currentX;
        lastY = currentY;
        currentX = event.getX();
        currentY = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                mCanvas.drawLine(lastX, lastY, currentX, currentY, eraserPaint);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return true;
    }
}
